#include <assert.h>
#include <stdio.h>
#include "test.h"
#include "../includes/type_datetime.h"
#include "../src/debug.h"

// Prototypes des fonctions
void DATETIME_IsDateValidTest();
void DATETIME_CreateTest();
void DATETIME_DestroyTest();
void DATETIME_CompareTest();
void DATETIME_SetGetDateTest();
void DATETIME_GetDaysInMonthTest();
void DATETIME_Add2YearsTest();
void DATETIME_Add2MonthsTest();
void DATETIME_Add62DaysTest();
void DATETIME_Substract365DaysTest();



int main(void)
{
  DATETIME_IsDateValidTest();
  DATETIME_CreateTest();
  DATETIME_DestroyTest();
  DATETIME_CompareTest();
  DATETIME_SetGetDateTest();
  DATETIME_GetDaysInMonthTest();
  DATETIME_Add2YearsTest();
  DATETIME_Add2MonthsTest();
  DATETIME_Add62DaysTest();
  DATETIME_Substract365DaysTest();
  
  return 0;
}

void DATETIME_CreateTest()
{
  TEST_TITLE("DATETIME_CreateTest");
  PTR_DATETIME ptrDateTime = NULL;
  int result = 0;
  result = DATETIME_Create(&ptrDateTime);
  
  TEST(result == 0);
  TEST(ptrDateTime != NULL);
}

void DATETIME_DestroyTest()
{
  TEST_TITLE("DATETIME_DestroyTest");
  PTR_DATETIME ptrDateTime = NULL;
  int result = 0;
  result = DATETIME_Create(&ptrDateTime);
  
  assert(result == 0);
  assert(ptrDateTime != NULL);
  
  result = DATETIME_Destroy(&ptrDateTime);
  TEST(result == 0);
  TEST(ptrDateTime == NULL);
}

void DATETIME_CompareTest()
{
  TEST_TITLE("DATETIME_CompareTest");
  PTR_DATETIME ptrDateTime1 = NULL;
  PTR_DATETIME ptrDateTime2 = NULL;
  int result = 0;
  result = DATETIME_Create(&ptrDateTime1);
  assert(result == 0);
  result = DATETIME_Create(&ptrDateTime2);
  assert(result == 0);
  
  int resultat_comparaison = -2;
  resultat_comparaison = DATETIME_Compare(ptrDateTime1, ptrDateTime2);
  
  TEST(resultat_comparaison == 0);
  
  DATETIME_Destroy(&ptrDateTime1);
  DATETIME_Destroy(&ptrDateTime2);
}

void DATETIME_SetGetDateTest()
{
  TEST_TITLE("DATETIME_SetGetDateTest");
  PTR_DATETIME ptrDateTime = NULL;
  int result = 0;
  result = DATETIME_Create(&ptrDateTime);
  
  DATETIME_SetDate(ptrDateTime, 2011, 10, 5);
  int year = 0, month = 0, day = 0;
  DATETIME_GetDate(ptrDateTime, &year, &month, &day);
  
  TEST(year == 2011);
  TEST(month == 10);
  TEST(day == 5);
  
  result = DATETIME_Destroy(&ptrDateTime);
}

void DATETIME_GetDaysInMonthTest()
{
  TEST_TITLE("DATETIME_GetDaysInMonthTest");
  TEST(DATETIME_GetDaysInMonth(2011,02) == 28);
  TEST(DATETIME_GetDaysInMonth(2000,02) == 29);
  TEST(DATETIME_GetDaysInMonth(1900,02) == 28);
  TEST(DATETIME_GetDaysInMonth(2011,12) == 31);
}

void DATETIME_IsDateValidTest()
{
  TEST_TITLE("DATETIME_IsDateValidTest");
  TEST(DATETIME_IsDateValid(2011,10,3) == 1);
  TEST(DATETIME_IsDateValid(2011,02,29) == 0);
  TEST(DATETIME_IsDateValid(2000,02,29) == 1);
}

void DATETIME_Add2YearsTest()
{
  TEST_TITLE("DATETIME_Add2YearsTest");
  PTR_DATETIME ptrDateTime = NULL;
  int result = 0;
  int year, month, day;
  result = DATETIME_Create(&ptrDateTime);
  
  assert(result == 0);
  DATETIME_SetDate(ptrDateTime, 2011, 10, 5);
  DATETIME_AddYear(ptrDateTime, 2);
  DATETIME_GetDate(ptrDateTime, &year, &month, &day);
  
  TEST(year == 2013);
  TEST(month == 10);
  TEST(day == 5);
  
  result = DATETIME_Destroy(&ptrDateTime);
}

void DATETIME_Add2MonthsTest()
{
  TEST_TITLE("DATETIME_Add2MonthsTest");
  PTR_DATETIME ptrDateTime = NULL;
  int result = 0;
  int year, month, day;
  result = DATETIME_Create(&ptrDateTime);
  
  assert(result == 0);
  DATETIME_SetDate(ptrDateTime, 2011, 11, 5);
  DATETIME_AddMonth(ptrDateTime, 2);
  DATETIME_GetDate(ptrDateTime, &year, &month, &day);
  
  TEST(year == 2012);
  TEST(month == 1);
  TEST(day == 5);
  
  result = DATETIME_Destroy(&ptrDateTime);
}

void DATETIME_Add62DaysTest()
{
  TEST_TITLE("DATETIME_Add62DaysTest");
  PTR_DATETIME ptrDateTime = NULL;
  int result = 0;
  int year, month, day;
  result = DATETIME_Create(&ptrDateTime);
  
  assert(result == 0);
  DATETIME_SetDate(ptrDateTime, 2011, 11, 5);
  DATETIME_AddDay(ptrDateTime, 62);
  DATETIME_GetDate(ptrDateTime, &year, &month, &day);
  
  TEST(year == 2012);
  TEST(month == 1);
  TEST(day == 6);
  
  result = DATETIME_Destroy(&ptrDateTime);
}

void DATETIME_Substract365DaysTest()
{
  TEST_TITLE("DATETIME_Substract365DaysTest");
  PTR_DATETIME ptrDateTime = NULL;
  int result = 0;
  int year, month, day;
  result = DATETIME_Create(&ptrDateTime);
  
  assert(result == 0);
  DATETIME_SetDate(ptrDateTime, 2011, 11, 5);
  DATETIME_AddDay(ptrDateTime, -365);
  DATETIME_GetDate(ptrDateTime, &year, &month, &day);
  
  TEST(year == 2010);
  TEST(month == 11);
  TEST(day == 5);
  
  result = DATETIME_Destroy(&ptrDateTime);
}